{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Linear Regression demo\n",
    "\n",
    "Code including two way to do linear regression:\n",
    "\n",
    "- Gradient Descent\n",
    "- Normal Equation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gradient Descent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import random\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cost_function(X, y, theta):\n",
    "    h = np.dot(X, theta)\n",
    "    difference = np.square(h - y)\n",
    "    cost = np.sum(difference) / (2 * len(y))\n",
    "    return cost\n",
    "\n",
    "def gradient_descent(X, y, theta, alpha):\n",
    "    h = np.dot(X, theta)\n",
    "    difference = h - y\n",
    "    theta = theta - (alpha / len(y)) * (np.dot(X.T, difference));\n",
    "    return theta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_iters = 1000\n",
    "alpha = 0.01  # Learning rate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAHfFJREFUeJzt3X1wVPW5B/Dvs0k2hCSamBdEwEIdSs11UCSlCvd2cGyRKrfUi9b6grRagaG22tEW+0LrXO7MFV/q6NxawJcKvlVv0atjfaOOjlN79TYRRAQRsbSgGGJMIITAJtnn/rFnl83mnN2zL+f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xX1V8nPeXJ89gqXlfOYSev9uZwdfjziF3pfxvAuwB+bmxfCmCp8VgA/Aax7oB3ALQWOebRiCXo45O2Jcd7rfHf8jZiF0JmuhzfYwD2ARhAbLRyNYAGAC8D2Gl8PcHYtxXAfUmvvQrAB8a/7xY55g8Qq4VuNv6tNvY9CcBz6c6fIsX7kHF+bkEs8YxNjdf4/nzEuiB2FTNeY/uD8fM2aV8vvL//jFiZZEvS7/98r57HaeJ1/BzmTFEiooDwVMmFiIhyx4RORBQQTOhERAHBhE5EFBBM6EREAcGETkQUEEzoREQBwYRORBQQ/w9es9V5wlmFvwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data = pd.read_csv('/data_set/ex1data1.txt', header=None)\n",
    "\n",
    "X = np.array(data[0])\n",
    "y = np.transpose([np.array(data[1])])\n",
    "sns.scatterplot(X.T, y.T[0])  # Show data distribution\n",
    "\n",
    "X = np.column_stack((np.ones(len(X)), X))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Randomly select theta for hypothesis\n",
    "theta = np.array([[random.uniform(y.min(), y.max()/2)], [random.uniform(-1, 1)]])\n",
    "# theta = np.array([[0],[0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Inital θ_0: 9.783382 θ_1: -0.858830\n",
      "After 1000 iterations, the cost is 4.943083 and the hypothesis is h(θ)=-1.6331+0.9657X\n"
     ]
    }
   ],
   "source": [
    "print(\"Inital θ_0: {:f} θ_1: {:f}\".format(theta[0][0], theta[1][0]))\n",
    "cost_array = []\n",
    "for _ in range(num_iters):\n",
    "    theta = gradient_descent(X, y, theta, alpha)\n",
    "    cost_array.append(cost_function(X, y, theta))\n",
    "print(\"After {:d} iterations, the cost is {:f} and the hypothesis is h(θ)={:.4f}+{:.4f}X\".format(num_iters, cost_function(X, y, theta), theta[0][0], theta[1][0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0,0.5,'Cost')"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYwAAAEWCAYAAAB1xKBvAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAIABJREFUeJzt3Xl4VPW9x/H3NytJCCGQsC9BNgUFlIgLirghtS5dVLRqqUtRa6ve6r217b3Vts+9tdfbqq1W0bpWRVuXat3RqrgiQQFBQEAChDUQSMISIOR7/5gDHWMSBsjkTCaf1/PMM3N+53cm35ODfnK23zF3R0REZG9Swi5ARERaBwWGiIjERIEhIiIxUWCIiEhMFBgiIhITBYaIiMREgSFtmpmVmtkpweefmdmfw65JJFEpMCRhmdn5ZjbdzLaY2brg8w/MzOLx89z9f9z98gP9HjMrMjM3s7Qm+txsZjvNrDp4fW5md5pZ9wP9+fESrNOAsOuQ8CgwJCGZ2fXAHcCtQDegK3AlMBrIaGSZ1BYrsHk86e65QCfgm0TWc2Yih4a0bQoMSThmlgf8CviBuz/l7tUe8Ym7X+ju24N+D5nZ3Wb2kpltAU40s6+b2SdmVmVmK8zs5nrffbGZLTOzDWb283rzbjazR6Omjzaz981sk5nNNrOxUfPeMrNfm9l7wR7Ca2ZWEMyeFrxvMrPNZnZMU+vr7jvdfR4wASgHro/6OWeY2ayghvfNbFjUvJ+Y2crg5y80s5OD9tTg8NqSYN5MM+sdzDvYzKaaWUWwzHlR3/eQmd1lZi8Gy003s/7BvN3rNDtYpwlNrZMkKXfXS6+EegH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ye2zApaLsFyAq+77GgbffejAXfPdfMPjWzCVHf/Zb965kXjwV3OIu0qLS9dxFp024EbnD3MwCC//FXuvuRZpYJvGdmrwV9RwGHuvvSYPpSd68wsyxghpk97e43mtkP3X1EAz/rW0Tu2h4OFATLTAvmHQ4MJTIO0HtExs16t/lXV6Rx2sMQ2TfjiIzZM4vIcPGdiTy0BuCjqLAAuMbMZgMfEhkAbiBNOw6Y4u673H0t8DZwZNR3l7l7HZFhXoqaZW1E9oH2MET2jQE/cvcvDeBmZmOJDC0ePX0KkQf3bDWzt4iMabS3727M9qjPu9B/uxIC7WGINK0ayI2afhW4Khg6HjMbFDywqL48Io8F3WpmBxN5TO5uO3cvX880YEJwnqQQGENksDyRhKC/UkSaNgeoDQ4tPUTk+dlFwMfBiedy4BsNLPcKcKWZzSHyuMwPo+bdC8wxs489Mvz6bs8SebTobCIjDf+Hu68JAkckdBqtVkREYqJDUiIiEhMFhoiIxESBISIiMVFgiIhITBQYIiISEwWGiIjERIEhIiIx+X+xW5uGtFiCAAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot cost change\n",
    "g = sns.lineplot(np.arange(num_iters), cost_array)\n",
    "plt.title('Gradient Descent')\n",
    "plt.xlabel('Iteration')\n",
    "plt.ylabel('Cost')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Normal Equation\n",
    "\n",
    "> Please make sure that your matrix X is a non-singular matrix before you run this code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv('ex1data1.txt', header=None)\n",
    "\n",
    "X = np.array(data[0])\n",
    "\n",
    "ne_X = np.column_stack((np.ones(len(X)), X))\n",
    "ne_y = np.transpose([np.array(data[1])])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "theta = np.dot(np.dot(np.linalg.inv(np.dot(ne_X.T, ne_X)), ne_X.T), y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The cost is 4.476971 and the hypothesis is h(θ)=-3.8958+1.1930X\n"
     ]
    }
   ],
   "source": [
    "print(\"The cost is {:f} and the hypothesis is h(θ)={:.4f}+{:.4f}X\".format(cost_function(ne_X, ne_y, theta), theta[0][0], theta[1][0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Credit\n",
    "\n",
    "Data `ex1data1.txt` is from Andrew Ng's Machine Learning Course"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
